<template>
  <div>
    <el-form ref="form">
      <el-form-item prop="search" class="mb20 mt10">
        <el-row type="flex" class="row-bg" justify="end">
          <el-col :xs="12" :sm="10" :md="8" :lg="6">
            <el-input
              v-model.number="search"
              size="mini"
              placeholder="输入客户号码或客户姓名搜索"
              @keyup.enter.native="getWarrants(15, 0)"
            ></el-input>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-search"
              @click="getWarrants(15, 0)"
            >搜索</el-button>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button type="success" size="mini" :icon="searchIcon" @click="advancedSearch()">高级搜索</el-button>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-circle-close-outline"
              @click="resetSearch"
            >重置</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <advancedSearch
      :advancedSearchs="advancedSearchs"
      v-show="showAdvancedSearch"
      @filter="handleFilter"
    ></advancedSearch>
    <warrantDialog
      :title="title"
      :editDialog="editDialog"
      :warrantForm="warrantForm"
      @close="hanleClose"
      @update="handleUpdate"
    ></warrantDialog>
    <el-table
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column type="selection" width="45"></el-table-column>
      <el-table-column prop="id" label="序号" width="65" fixed></el-table-column>
      <el-table-column sortable prop="createdAt" label="创建时间" width="140" fixed>
        <template slot-scope="scope">{{scope.row.createdAt | moment("YYYY/MM/DD h:mm:ss")}}</template>
      </el-table-column>
      <el-table-column prop="phone" label="客户电话" width="110" fixed></el-table-column>
      <el-table-column prop="name" label="客户姓名" width="70" fixed></el-table-column>
      <el-table-column prop="promotionName" label="推广渠道" width="70"></el-table-column>
      <el-table-column prop="deposit" label="定金(元)" width="70"></el-table-column>
      <el-table-column prop="warrantMan" label="权证专员" width="70"></el-table-column>
      <el-table-column prop="loanAmount" label="放款金额(元)" width="85"></el-table-column>
      <el-table-column prop="centerName" label="中心" width="50"></el-table-column>
      <el-table-column prop="deptName" label="部门" width="50"></el-table-column>
      <el-table-column prop="salesmanName" label="业务员" width="70"></el-table-column>
      <el-table-column prop="pickCarTime" label="提车时间" width="90"></el-table-column>
      <el-table-column prop="passTime" label="审过时间" width="90"></el-table-column>
      <el-table-column prop="cartype" label="购买车型" width="70"></el-table-column>
      <el-table-column prop="trial" label="审过渠道" width="80"></el-table-column>
      <el-table-column prop="notpass" label="未审过原因" width="85"></el-table-column>
      <el-table-column prop="cancellation" label="退单原因" width="100"></el-table-column>
      <el-table-column prop="operation" label="操作" width="110">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
            icon="el-icon-edit-outline"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="mt20">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pages"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import warrantDialog from "@/components/warrant_dialog";
import advancedSearch from "@/components/advanced_search";

export default {
  name: "warrant",
  data() {
    return {
      page: 15,
      total: 0,
      search: "",
      currentPage: 1,
      pages: [15, 20, 25, 35],
      title: "",
      tableData: [],
      loading: true,
      editDialog: false,
      advancedSearchs: ["pass", "pick", "deposit_to_achievement"],
      currentPage: 3,
      val: 1,
      warrantMan: "",
      pass: "",
      pick: "",
      depositToAchievement: "",
      warrantForm: {},
      showAdvancedSearch: false,
      advanced_search: "",
      searchIcon: "el-icon-arrow-down"
    };
  },
  created() {
    this.getWarrants(15, 0);
  },
  methods: {
    advancedSearch() {
      this.searchIcon === "el-icon-arrow-down"
        ? (this.searchIcon = "el-icon-arrow-up")
        : (this.searchIcon = "el-icon-arrow-down");
      this.showAdvancedSearch = !this.showAdvancedSearch;
    },
    handleFilter(arg) {
      this.advanced_search = arg.search;
      this.warrantMan = arg.warrantMan;
      this.pass = arg.pass;
      this.pick = arg.pick;
      this.depositToAchievement = arg.depositToAchievement;
      this.getWarrants(15, 0);
    },
    handleSizeChange(val) {
      this.page = val;
      this.getWarrants(val, 0);
    },
    handleCurrentChange(val) {
      this.val = val;
      // 下一页页数*每页显示行-上一页页数*每页显示行
      this.getWarrants(this.page, (val - 1) * this.page);
    },
    getWarrants(limit, offset) {
      this.$axios
        .get(
          `/api/warrant/warrants/?limit=${limit}&offset=${offset}&search=${this
            .search || this.advanced_search}&warrant=${this.warrantMan}&pass=${
            this.pass
          }&pick=${this.pick}&depositToAchievement=${this.depositToAchievement}`
        )
        .then(data => {
          this.tableData = data.doc;
          this.total = data.total;
          this.loading = false;
        });
    },
    resetSearch() {
      this.search = "";
      this.getWarrants(15, 0);
    },
    hanleClose() {
      this.editDialog = false;
      this.resetForm();
    },
    handleEdit(item) {
      this.editDialog = !this.editDialog;
      this.title = "编辑订单";
      this.warrantForm = item;
    },
    resetForm() {
      this.warrantForm = {
        time: "",
        promotion: "",
        center: "",
        dept: "",
        salesman: ""
      };
    },
    handleUpdate() {
      this.getWarrants(this.page, (this.val - 1) * this.page);
    }
  },
  components: {
    warrantDialog,
    advancedSearch
  }
};
</script>
<style>
</style>
